package org.nf.box.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.box.domain.PersonalInfo;
import org.nf.box.mapper.PersonalInfoMapper;
import org.nf.box.mapper.UserMapper;
import org.nf.box.service.PersonalInfoService;
import org.nf.box.web.vo.PersonalInfoVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzp
 * @Date 2024-12-12
 * 个人资料业务
 */
@Service("personalInfoService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class)
@Slf4j
public class PersonalInfoServiceImpl implements PersonalInfoService {

    private final PersonalInfoMapper personalInfoMapper;
    private final UserMapper userMapper;

    @Override
    public PersonalInfo getPersonalInfoById(Integer userId) {
        return personalInfoMapper.getPointerInfoById(userId);
    }

    @Override
    public Map<Integer, PersonalInfo> getPointerInfosByIds(List<Integer> userIds) {
        Map<Integer, PersonalInfo> personalInfoMap = new HashMap<>();
        if (userIds == null || userIds.isEmpty()) {
            return personalInfoMap;
        }
        // 批量查询用户资料
        List<PersonalInfo> personalInfos = personalInfoMapper.getPointerInfoByIds(userIds);
        // 将结果填充到映射中
        for (PersonalInfo info : personalInfos) {
            personalInfoMap.put(info.getUserId(), info);
        }
        return personalInfoMap;
    }

    @Override
    public PersonalInfoVO getPointerInfoVOById(Integer userId) {
        return personalInfoMapper.getPointerInfoVOById(userId);
    }

    @Override
    public void updatePointerInfo(PersonalInfo personalInfo) {
        personalInfoMapper.updatePointerInfo(personalInfo);
        //同步用户表
        userMapper.updateNickname(personalInfo.getUserId(),personalInfo.getNickName());
    }

    @Override
    public void addPointerInfo(PersonalInfo personalInfo) {
        personalInfoMapper.addPointerInfo(personalInfo);
    }

}
